package com.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.JdInventoryMapper;
import com.pojo.JdInventory;
import com.pojo.JdInventoryExample;
import com.pojo.JdWarehouse;
import com.pojo.Product;
import com.vo.InventoryVO;
import com.vo.QueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class InventoryService {
    
    @Resource
    private JdInventoryMapper inventoryMapper;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private ProductService productService;
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public JdInventory queryByKey(int i_num){
        return inventoryMapper.selectByPrimaryKey(i_num);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public List<JdInventory> queryByType(JdInventory inventory){
        JdInventoryExample example = new JdInventoryExample();
        JdInventoryExample.Criteria criteria = example.createCriteria();
        if (inventory != null) {
            if (inventory.getP_id()!=null && !"".equals(inventory.getP_id())){
                criteria.andP_idEqualTo(inventory.getP_id());
            }
        }
        return inventoryMapper.selectByExample(example);
    }
    
    //库存列表
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public PageInfo<InventoryVO> queryByPage(Integer pageNum , Integer pageSize , QueryVo vo) {
        
        //获取名称
        JdWarehouse warehouse = null;
        if (vo.getW_name() != null && !"".equals(vo.getW_name().trim())){
            List<JdWarehouse> warehouses = warehouseService.queryByType(new JdWarehouse(vo.getW_name().trim()));
            if (!warehouses.isEmpty()){
                warehouse = warehouses.get(0);
            }else{
                warehouse = new JdWarehouse(-1);
            }
        }
    
        Product product = null;
        if (vo.getP_id() != null && !"".equals(vo.getP_id().trim())){
            List<Product> products = productService.queryByType(new Product(vo.getP_id().trim()));
            if (!products.isEmpty()){
                product = products.get(0);
            }else {
                product = new Product("null");
            }
        }
        
        JdInventoryExample example = new JdInventoryExample();
        JdInventoryExample.Criteria criteria = example.createCriteria();
        if(vo != null){
            if (product != null){
                criteria.andP_idEqualTo(product.getP_id().trim());
            }
            if (warehouse != null){
                criteria.andW_idEqualTo(warehouse.getW_id());
            }
        }
        PageHelper.startPage(pageNum , pageSize);
        List<JdInventory> list = inventoryMapper.selectByExample(example);
        List<InventoryVO> inventoryVOList = new ArrayList<>();
        if (list.isEmpty()){
            return new PageInfo<>(inventoryVOList);
        }
        for (JdInventory inventory : list) {
            
            InventoryVO inventoryVO = new InventoryVO();
        
            List<Product> products = productService.queryByType(new Product(inventory.getP_id()));
            Product productTmp = null;
            if (!products.isEmpty()){
                productTmp = products.get(0);
            }
            
            List<JdWarehouse> warehouses = warehouseService.queryByType(new JdWarehouse(inventory.getW_id()));
            JdWarehouse warehouseTmp = null;
            if (!products.isEmpty()){
                warehouseTmp = warehouses.get(0);
            }
            
            inventoryVO.setI_num(inventory.getI_num());
            if (productTmp!=null){
                inventoryVO.setP_id(productTmp.getP_id());
                inventoryVO.setP_name(productTmp.getP_name());
            }
            if (warehouseTmp!=null){
                inventoryVO.setW_name(warehouseTmp.getW_name());
            }
            inventoryVO.setI_InventoryNum(inventory.getI_InventoryNum());
            inventoryVO.setI_inpNum(inventory.getI_InpNum());
            inventoryVO.setI_outNum(inventory.getI_outNum());
        
            inventoryVOList.add(inventoryVO);
        }
        return new PageInfo<>(inventoryVOList);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int update(JdInventory inventory) {
        if(inventory != null){
            return inventoryMapper.updateByPrimaryKeySelective(inventory);
        }
        return 0;
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int addInventory(JdInventory inventory) {
        if (inventory == null) {
            return 0;
        }
        return inventoryMapper.insertSelective(inventory);
    }
    
}
